package com.thrsdk.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import dy.com.github.gzuliyujiang.oaid.DeviceID;
import dy.com.github.gzuliyujiang.oaid.DeviceIdentifier;
import dy.com.github.gzuliyujiang.oaid.IGetter;

public class DyPhoneUtils {
    public static String oaid = "";


    public static String getOaid() {
        return oaid;
    }

    public static boolean emptyOrUnknow(String str) {
        return TextUtils.isEmpty(str) || "unknown".equals(str) || str.startsWith("0000") || "9774d56d682e549c".equals(str);
    }

    public static void isReadyAndroidOAID(Context context, OaidListener oaidListener) {
        if (DeviceID.supportedOAID(context)) {
            // 获取OAID，华为 小米等同步调用可以直接获取
            String firststring = DeviceIdentifier.getOAID(context);
            if (!TextUtils.isEmpty(firststring)) {
                if (null != oaidListener) {
                    oaid = firststring;
                    oaidListener.OnGet(firststring);
                    return;
                }
            }
            // 获取OAID，异步步调用（OPPO VIVO 异步获取）
            DeviceID.getOAID(context, new IGetter() {
                @Override
                public void onOAIDGetComplete(String result) {
                    if (null != oaidListener) {
                        oaid = result;
                        oaidListener.OnGet(result);
                    }

                }

                @Override
                public void onOAIDGetError(Exception error) {
                    // 获取OAID/AAID失败
                    if (null != oaidListener) {
                        oaid = "";
                        oaidListener.OnGet("");
                    }

                }
            });
        } else {
            if (null != oaidListener) {
                oaid = "";
                oaidListener.OnGet("");
            }
        }

    }

    private static final char[] hexDigits =
            {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    public static String toHexString(byte[] bytes) {
        if (bytes == null) return "";
        StringBuilder hex = new StringBuilder(bytes.length * 2);
        for (byte b : bytes) {
            hex.append(hexDigits[(b >> 4) & 0x0F]);
            hex.append(hexDigits[b & 0x0F]);
        }
        return hex.toString();
    }

    private final static String SP_NAME = "base_config_fusion";
    private final static String key = "fusionDeviceUniqueId";


    public static String initDeviceUniqueId(Context context) {
        //        需要缓存 确保只要不是重启 都应该不变
        SharedPreferences sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        String deviceId = sp.getString(key, "");
        if (!TextUtils.isEmpty(deviceId)) {
            //"使用缓存的DeviceUniqueId：" + deviceId);
            return deviceId;
        }
        //后面安卓10以上改成  OAID    安卓ID获取自己的设备唯一标识

        //获取自己的设备唯一标识
        deviceId = oaid;
        if (emptyOrUnknow(deviceId)) {
            deviceId = DeviceIdentifier.getAndroidID(context);

        }
        if (emptyOrUnknow(deviceId)) {
            deviceId = DeviceIdentifier.getGUID(context);

        }
        //首次的DeviceUniqueId
        SharedPreferences sp2 = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        sp2.edit().putString(key, deviceId).apply();
        return deviceId;
    }


}